import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import store from '@/store'
export const routes = [
  // 登录
  {
    path: '/login',
    component: () => import('@/views/Login/index'),
    hidden: true
  },
  // 首页
  {
    path: '/',
    component: Layout,
    redirect: '/workbench'
  },
  // 首页
  {
    path: '/workbench',
    component: Layout,
    children: [{
      path: '',
      component: () => import('@/views/Workbench/index'),
      meta: { title: '首页' }
    }]
  },
  // 线索管理
  {
    path: '/clue',
    component: Layout,
    children: [{
      path: '',
      component: () => import('@/views/Clue/index'),
      meta: { title: '线索管理' }
    }]
  },
  // 线索跟进
  {
    path: '/with',
    component: Layout,
    hidden: true,
    children: [{
      path: '',
      component: () => import('@/views/Clue/with/index'),
      meta: { title: '线索跟进' }
    }]
  },
  // 商机管理
  {
    path: '/commerce',
    component: Layout,
    children: [{
      path: '',
      component: () => import('@/views/Commerce/index'),
      meta: { title: '商机管理' }
    }]
  },
  // 商机详情
  {
    path: '/detail',
    component: Layout,
    hidden: true,
    children: [{
      path: '',
      component: () => import('@/views/Commerce/components/Detail'),
      meta: { title: '商机详情' }
    }]
  },
  // 商机跟进
  {
    path: '/adddetail',
    component: Layout,
    hidden: true,
    children: [{
      path: '',
      component: () => import('@/views/Commerce/components/addDetail'),
      meta: { title: '商机跟进' }
    }]
  },
  // 合同管理
  {
    path: '/contract',
    component: Layout,
    children: [{
      path: '',
      component: () => import('@/views/Contract/index'),
      meta: { title: '合同管理' }
    }]
  },
  // 基础数据维护
  {
    path: '/maindata',
    component: Layout,
    permission: 'MainData',
    meta: { title: '基础数据维护' },
    children: [{
      path: 'course',
      meta: { title: '课程管理' },
      component: () => import('@/views/MainData/Course/index')
    },
    {
      path: 'activities',
      meta: { title: '活动管理' },
      component: () => import('@/views/MainData/Activities/index')
    }]
  },
  // 转派管理
  {
    path: '/allocation',
    component: Layout,
    children: [{
      path: '',
      component: () => import('@/views/Allocation/index'),
      meta: { title: '转派管理' }
    }]
  },
  // 统计分析
  {
    path: '/analysis',
    component: Layout,
    children: [{
      path: '',
      component: () => import('@/views/Analysis/index'),
      meta: { title: '统计分析' }
    }]
  },
  // 系统管理
  {
    path: '/system',
    component: Layout,
    meta: { title: '系统管理' },
    children: [{
      path: 'message',
      meta: { title: '通知中心' },
      // hidden: true,
      component: () => import('@/views/System/Message/index')
    },
    {
      path: 'permissions',
      meta: { title: '权限管理' },
      component: () => import('@/views/System/Permissions/index'),
      children: [{
        path: 'user',
        meta: { title: '用户管理' },
        component: () => import('@/views/System/Permissions/User/index')
      },
      {
        path: 'role',
        meta: { title: '角色管理' },
        component: () => import('@/views/System/Permissions/Role/index')
      },
      {
        path: 'menu',
        meta: { title: '菜单管理' },
        component: () => import('@/views/System/Permissions/Menu/index')
      },
      {
        path: 'department',
        meta: { title: '部门管理' },
        component: () => import('@/views/System/Permissions/Department/index')
      },
      {
        path: 'position',
        meta: { title: '岗位管理' },
        component: () => import('@/views/System/Permissions/Position/index')
      }
      ]
    },
    {
      path: 'dictionary',
      meta: { title: '字典管理' },
      component: () => import('@/views/System/Dictionary/index')
    },
    {
      path: 'clueConfig',
      meta: { title: '线索配置' },
      component: () => import('@/views/System/ClueConfig/index')
    }
    ]
  },
  // 线索配置
  {
    path: '/editClub',
    component: Layout,
    children: [{
      path: '',
      component: () => import('@/views/Clue/editClub'),
      meta: { title: '线索配置' }
    }]
  },
  // 商机配置
  {
    path: '/editCommerce',
    component: Layout,
    children: [{
      path: '',
      component: () => import('@/views/Commerce/editCommerce'),
      meta: { title: '商机配置' }
    }]
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  // mode: 'hash',
  scrollBehavior: () => ({ y: 0 }),
  routes: routes
})

const router = createRouter()

// 重置路由方法
export function resetRouter() {
  // 得到一个全新的router实例对象
  const newRouter = createRouter()
  // 使用新的路由记录覆盖掉老的路由记录
  router.matcher = newRouter.matcher
}

// 前置守卫
// 白名单
const writeList = ['/login', '/404']

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      const matchedRoutes = to.matched // 获取当前路由的匹配路由数组
      const breadcrumbItems = matchedRoutes.map(route => {
        return {
          title: route.meta.title || '主页',
          path: route.path
        }
      })
      store.commit('set', breadcrumbItems)
      next()
    }
  } else {
    if (writeList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
